package com.imooc.controller;


import com.imooc.pojo.Users;
import com.imooc.pojo.vo.UsersVo;
import com.imooc.service.UserService;
import com.imooc.utils.FileUtils;
import com.imooc.utils.IMoocJSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@Api(value = "用户相关", tags = {"用户相关"})
@RequestMapping("/user")
public class UserController extends BaseController {
    @Autowired
    private UserService userService;

    @PostMapping("/info")
    @ApiOperation(value = "获取用户信息", notes = "根据条件获取用户信息")
    @ApiImplicitParam(name = "username", value = "用户name", required = true, dataType = "String", paramType = "query")
    public IMoocJSONResult info(String username) {
        //判断名字不为空
        return IMoocJSONResult.ok();
    }

    @Value("${com.imooc.fileSpace}")
    private String fileSpace;

    @PostMapping("/uploadFace")
    @ApiOperation(value = "上传图片", notes = "上传图片")
    @ApiImplicitParam(name = "userId", value = "userId", required = true, dataType = "String", paramType = "form")
    public IMoocJSONResult uploadFace(String userId, @RequestParam("file") MultipartFile[] files) throws IOException {
// TODO: 2020/5/29 userId需要判断
        //判断传递的文件是不是空的
        if (files == null || files.length == 0) {
            return IMoocJSONResult.errorMsg("请选择文件 ");
        }

        //数据库相对路径
        String uploadPathDB = "/" + userId + "/face";

        uploadPathDB = FileUtils.saveFile(files, fileSpace, uploadPathDB);

        //保存到数据库中
        Users users = new Users();
        users.setId(userId);
        users.setFaceImage(uploadPathDB);
        userService.updateUserInfo(users);


        return IMoocJSONResult.ok(uploadPathDB);
    }

    /**
     * 查询用户信息 根据用户id
     */
    @PostMapping("/query")
    @ApiOperation(value = "查询用户信息", notes = "根据用户id查询用户信息")
    @ApiImplicitParam(name = "userId", value = "userId", required = true, dataType = "String", paramType = "query")
    public IMoocJSONResult query(String userId) {

        if (StringUtils.isBlank(userId)) {
            return IMoocJSONResult.errorMsg("用户id不能为空");
        }
        Users users = userService.queryUserInfo(userId);
        if (users == null) {
            return IMoocJSONResult.errorMsg("查询失败");
        } else {
            UsersVo usersVo = new UsersVo();
            BeanUtils.copyProperties(users, usersVo);
            usersVo.setPassword("");
            System.out.println(usersVo.toString());
            return IMoocJSONResult.ok(usersVo);
        }


    }

    /**
     * 查看是否关注
     */
    @PostMapping("/isFans")
    @ApiOperation(value = "查看是否关注", notes = "查看是否关注")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "userId", required = true, dataType = "String", paramType = "query")
            ,
            @ApiImplicitParam(name = "fanId", value = "fanId", required = true, dataType = "String", paramType = "query")

    })
    public IMoocJSONResult isFans(String userId, String fanId) {
        Boolean aBoolean = userService.isFans(userId, fanId);
        return IMoocJSONResult.ok(aBoolean);
    }

    /**
     * 关注
     */
    @PostMapping("/addFansCounts")
    @ApiOperation(value = "关注", notes = "关注")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "userId", required = true, dataType = "String", paramType = "query")
            ,
            @ApiImplicitParam(name = "fanId", value = "fanId", required = true, dataType = "String", paramType = "query")

    })
    public IMoocJSONResult addFansCounts(String userId, String fanId) {
        Boolean aBoolean = userService.addFansCounts(userId, fanId);
        return IMoocJSONResult.ok(aBoolean);
    }


    /**
     * 取消关注
     */
    @PostMapping("/reduceFansCounts")
    @ApiOperation(value = "取消关注", notes = "取消关注")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "userId", required = true, dataType = "String", paramType = "query")
            ,
            @ApiImplicitParam(name = "fanId", value = "fanId", required = true, dataType = "String", paramType = "query")

    })
    public IMoocJSONResult reduceFansCounts(String userId, String fanId) {
        Boolean aBoolean = userService.reduceFansCounts(userId, fanId);
        return IMoocJSONResult.ok(aBoolean);
    }
}
